package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.org.domain.Product;
import cn.itsource.pethome.org.query.ProductQuery;
import cn.itsource.pethome.org.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description: 描述:
 * @date: 2020/8/9 14:22
 * @author: summer
 * version: 1.0
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @PatchMapping
    public PageList<Product> findAll(@RequestBody ProductQuery productQuery){
        return productService.findAll(productQuery);
    }

    @DeleteMapping("{id}")
    public AjaxResult delProduct(@PathVariable("id") Long id){
        try {
            productService.delProduct(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！");
        }
    }

    @PatchMapping("/batchDelProduct")
    public AjaxResult batchDelProduct(@RequestBody Long[] id){
        try {
            productService.batchDelProduct(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！");
        }
    }

    @PutMapping
    public AjaxResult save(@RequestBody Product product){
        try {
            productService.save(product);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "添加失败");
        }
    }

    @PostMapping
    public AjaxResult update(@RequestBody Product product){
        try {
            productService.update(product);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "修改失败");
        }
    }
    //上架
    @PatchMapping("/productAdd")
    public AjaxResult productAdd(@RequestBody Long[] id){
        try {
            productService.productAdd(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
    //下架
    @PatchMapping("/productDown")
    public AjaxResult productDown(@RequestBody Long[] id){
        try {
            productService.productDown(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    @PatchMapping("/particulars/{id}")
    public Product particulars(@PathVariable("id")Long id){
        return productService.particulars(id);
    }
}
